#!/bin/bash
#
# 08/08/2012 - created

SCALR_DIR=/home/scalr



#Download: 
#https://github.com/Scalr/scalr/tarball/master


sudo yum -y update
sudo yum -y install mysql-server
sudo yum -y install httpd
sudo yum -y install php
sudo yum -y install mysql
sudo yum -y install libssh2
sudo yum -y install bind
sudo yum -y install openssl


sudo rpm -ivh http://download.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-7.noarch.rpm

sudo yum update
sudo yum -y install php-mcrypt



# install PHP packages required by Scalr
sudo yum -y install rrdtool
sudo yum -y install rrdtool-php
sudo yum -y install php-gettext
sudo yum -y install php-mysqli
sudo yum -y install php-curl
sudo yum -y install php-rrdtool
sudo yum -y install php-posix
sudo yum -y install php-dom
sudo yum -y install php-snmp
sudo yum -y install php-soap
sudo yum -y install php-devel
sudo yum -y install libcurl-devel.x86_64
sudo yum -y install gcc
sudo yum -y install zlib-devel
sudo yum -y install make
sudo yum install php-pecl-apc

sudo pecl install pecl_http

# create Scalr user and user group
sudo useradd scalr -U -d /home/scalr
sudo chown scalr:scalr /home/scalr

sudo su - 

cd /home/scalr
tar xvzf /tmp/scalr-3.5.r7704.tar.gz 

mkdir current
ln -s  /home/scalr/scalr-3.5.r7704/* /home/scalr/current/
chown -R scalr:scalr current scalr-3.5.r7704/


# configure Apache 
cd /var/www/html/

cp -R /home/scalr/current/app/* .
exit

sudo chown apache:apache /var/www/html/cache
sudo chmod 700  /var/www/html/cache


# configure MySQL and create database
sudo useradd mysql -U -d /home/mysql

sudo chown mysql:mysql /var/lib/mysql/

# Edit /etc/my.cnf and add: 

[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
user=mysql
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0

port = 3306
socket = /var/lib/mysql/mysql.sock
skip-esternal-locking
skip-networking
skip-innodb

variable-name = key_buffer=16M
variable-name = max_allowed_packet=1M
variable-name = table_cache=64
variable-name = sort_buffer=512K
variable-name = net_buffer_length=8K
variable-name = myisam_sort_buffer_size=8M
server-id = 1



[mysqld_safe]
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid


sudo /usr/bin/mysql_install_db
sudo /etc/init.d/mysql start

create database scalr;


# Edit /home/scalr/current/sql/scalr.sql  file and replace CREATE TABLE with this:

CREATE TABLE IF NOT EXISTS `role_images` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `role_id` int(11) NOT NULL,
  `cloud_location` varchar(25) DEFAULT NULL,
  `image_id` varchar(255) DEFAULT NULL,
  `platform` varchar(25) DEFAULT NULL,
  `architecture` varchar(255) DEFAULT NULL,
  `os_family` varchar(255) DEFAULT NULL,
  `os_name` varchar(60) DEFAULT NULL,
  `os_version` varchar(60) DEFAULT NULL,
  `agent_version` varchar(100) DEFAULT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `unique` (`role_id`,`image_id`,`cloud_location`),
  UNIQUE KEY `role_id_location` (`role_id`,`cloud_location`),
  KEY `NewIndex1` (`platform`),
  KEY `location` (`cloud_location`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;


mysql -u root scalr < /home/scalr/current/sql/scalr.sql


# Edit /etc/php.ini, add the lines below and restart httpd

short_open_tag = On

; scalr required extension
extension=http.so




sudo mkdir /home/rrddata /home/graphics /home/scalr/current/logs
sudo chown scalr:scalr /home/rrddata /home/graphics /home/scalr/current/logs


cd /var/www/html/etc
sudo cp config.ini-sample config.ini

# Edit config.ini file



sudo vi /etc/httpd/conf/httpd.conf
DocumentRoot "/var/www/html/www"
AllowOverride All


sudo /etc/init.d/httpd restart



# add to root crontab 

* * * * * /usr/bin/php /var/www/html/cron-ng/cron.php --SNMPStatsPoller > /home/scalr/current/logs/SNMPStatsPoller.log 2>&1
*/2 * * * * /usr/bin/php /var/www/html//cron-ng/cron.php --Poller > /home/scalr/current/logs/Poller.log 2>&1
* * * * * /usr/bin/php /var/www/html/cron/cron.php --Scheduler2 > /home/scalr/current/logs/Scheduler2.log 2>&1
*/10 * * * * /usr/bin/php /var/www/html/cron/cron.php --MySQLMaintenance > /home/scalr/current/logs/MySQLMaintenance.log 2>&1
* * * * * /usr/bin/php /var/www/html/cron/cron.php --DNSManagerPoll > /home/scalr/current/logs/DNSManagerPoll.log 2>&1
17 5 * * * /usr/bin/php /var/www/html/cron/cron.php --RotateLogs > /home/scalr/current/logs/RotateLogs.log 2>&1
*/2 * * * * /usr/bin/php /var/www/html/cron/cron.php --EBSManager > /home/scalr/current/logs/EBSManager.log 2>&1
*/20 * * * * /usr/bin/php /var/www/html/cron/cron.php --RolesQueue > /home/scalr/current/logs/RolesQueue.log 2>&1
*/5 * * * * /usr/bin/php /var/www/html/cron-ng/cron.php --DbMsrMaintenance > /home/scalr/current/logs/DbMsrMaintenance.log 2>&1
*/2 * * * * /usr/bin/php /var/www/html/cron-ng/cron.php --Scaling > /home/scalr/current/logs/Scaling.log 2>&1
*/5 * * * * /usr/bin/php /var/www/html/cron/cron.php --DBQueueEvent > /home/scalr/current/logs/DBQueueEvent.log 2>&1
*/2 * * * * /usr/bin/php /var/www/html/cron/cron.php --SzrMessaging > /home/scalr/current/logs/SzrMessaging.log 2>&1
*/4 * * * * /usr/bin/php /var/www/html/cron/cron.php --RDSMaintenance > /home/scalr/current/logs/RDSMaintenance.log 2>&1
*/2 * * * * /usr/bin/php /var/www/html/cron/cron.php --BundleTasksManager > /home/scalr/current/logs/BundleTasksManager.log 2>&1
* * * * * /usr/bin/php /var/www/html/cron-ng/cron.php --ScalarizrMessaging > /home/scalr/current/logs/ScalarizrMessaging.log 2>&1
* * * * * /usr/bin/php /var/www/html/cron-ng/cron.php --MessagingQueue > /home/scalr/current/logs/MessagingQueue.log 2>&1
*/2 * * * * /usr/bin/php /var/www/html/cron-ng/cron.php --DeployManager > /home/scalr/current/logs/DeployManager.log 2>&1
*/2 * * * * /usr/bin/php /var/www/html/cron/cron.php --UsageStatsPoller > /home/scalr/current/logs/UsageStatsPoller.log 2>&1





sudo mkdir ${JETTY_DIR}
sudo chown 500:500 ${JETTY_DIR}
cd ${JETTY_DIR}

wget http://download.eclipse.org/jetty/7.6.1.v20120215/dist/jetty-distribution-7.6.1.v20120215.tar.gz
tar zxvf jetty-distribution-7.6.1.v20120215.tar.gz
rm -f jetty-distribution-7.6.1.v20120215.tar.gz

sudo yum -y install java

cd ${JETTY_DIR}/jetty-distribution-7.6.1.v20120215

nohup java -jar start.jar > start.log 2>&1 &

exit
